package org.springblade.task.job.base;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.common.utils.DateUtils;
import org.springblade.task.base.entity.BaseTaskLog;
import org.springblade.task.base.entity.BaseTaskInfo;
import org.springblade.task.base.param.BaseTaskLogParam;
import org.springblade.task.base.service.IBaseTaskLogService;
import org.springblade.task.base.service.IBaseTaskInfoService;
import org.springblade.task.quartz.entity.QuartzTask;
import org.springblade.task.quartz.service.IQuartzTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 提取任务活动类型信息
 * @author lzh
 */
@Component
public class BaseTaskInfoJob {
	private static Logger logger = LoggerFactory.getLogger(BaseTaskInfoJob.class);

	@Autowired
	private IQuartzTaskService quartzTaskService;
	@Autowired
	private IBaseTaskLogService BaseTaskLogService;
	@Autowired
	private IBaseTaskInfoService baseTaskInfoService;

	/**
	 * 1、简单任务示例（Bean模式）
	 */
	@XxlJob("baseTaskInfoJob")
	public ReturnT<String> baseTaskInfoJob(String param) throws Exception {
		System.out.println(DateUtils.currentDateTime() + " -- XXL-JOB, Hello World.");
		System.err.println("执行静态定时任务日期: " + BaseTaskInfoJob.class.getSimpleName());
		QuartzTask quartzTask = quartzTaskService.queryByTaskName(BaseTaskInfoJob.class.getSimpleName(), -7);
		// 今日
		String todayDate = DateUtils.crutDate();
		String taskDate = quartzTask.getTaskDate();
		while (taskDate.compareTo(todayDate) < 0) {
			// 当时间大于5点，跳出执行
//			if (DateUtils.getHour24() > 5) {
//				break;
//			}
			// 再加一个凌晨1点判断
			currMethod(taskDate);
			// 增加1天
			taskDate = DateUtils.dateAddDay(taskDate, 1);
		}
		quartzTask.setTaskDate(taskDate);
		quartzTaskService.saveQuartzTask(quartzTask);
		return ReturnT.SUCCESS;
	}

	/**
	 *
	 * @param taskDate
	 */
	public void currMethod(String taskDate) {

		BaseTaskLogParam param = new BaseTaskLogParam();
		param.setStartDate(DateUtils.getDateStartTime(taskDate));
		param.setEndDate(DateUtils.getDayEndTime(taskDate));
		List<BaseTaskLog> activityLogList = BaseTaskLogService.queryGroupByTaskNameActivityLog(param);
		if(!CollectionUtils.isEmpty(activityLogList)) {
			Set<BaseTaskInfo> taskInfoSet = new HashSet<>();
			BaseTaskInfo taskInfo = null;
			for (BaseTaskLog BaseTaskLog: activityLogList) {
				taskInfo = new BaseTaskInfo();
				taskInfo.setTaskId(BaseTaskLog.getTaskId());
				taskInfo.setTaskName(BaseTaskLog.getTaskName());
				taskInfo.setTaskType(BaseTaskLog.getTaskType());
				taskInfo.setUpdateTime(new Date());
				taskInfoSet.add(taskInfo);
			}
			baseTaskInfoService.saveTaskInfo(new ArrayList<>(taskInfoSet));
		}
	}


	/**
	 * 2、分片广播任务
	 */
	@XxlJob("shardingJobHandler")
	public ReturnT<String> shardingJobHandler(String param) throws Exception {
		return ReturnT.SUCCESS;
	}


	/**
	 * 3、命令行任务
	 */
	@XxlJob("commandJobHandler")
	public ReturnT<String> commandJobHandler(String param) throws Exception {
		return IJobHandler.SUCCESS;
	}


	/**
	 * 4、跨平台Http任务
	 *  参数示例：
	 *      "url: http://www.baidu.com\n" +
	 *      "method: get\n" +
	 *      "data: content\n";
	 */
	@XxlJob("httpJobHandler")
	public ReturnT<String> httpJobHandler(String param) throws Exception {
		return ReturnT.SUCCESS;
	}

	/**
	 * 5、生命周期任务示例：任务初始化与销毁时，支持自定义相关逻辑；
	 */
	@XxlJob(value = "demoJobHandler2", init = "init", destroy = "destroy")
	public ReturnT<String> demoJobHandler2(String param) throws Exception {
		XxlJobLogger.log("XXL-JOB, Hello World.");
		return ReturnT.SUCCESS;
	}

	public void init() {
		logger.info("init");
	}

	public void destroy() {
		logger.info("destory");
	}


}
